package com.neusoft.tyut.elm.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.neusoft.tyut.elm.util.AuthcodeTool;

/**
 * Servlet implementation class AuthCodeServlet
 * http://127.0.0.1:8080/elm/guest/authcode
 */
@WebServlet("/guest/authcode")
public class AuthCodeServlet extends HttpServlet {

    public static final int WIDTH = 120;//生成的图片的宽度
    public static final int HEIGHT = 30;//生成的图片的高度
	
	private static final long serialVersionUID = 1L; 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 response.setContentType("image/jpg");
		 String authcode =  AuthcodeTool.authcode();
		 HttpSession session = request.getSession(); // SessionID 以Cookie形式保存在前端；		
		 session.setAttribute(AuthcodeTool.AUTHCODE, authcode);
		 
		   //1.在内存中创建一张图片
	        BufferedImage bi = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
	        //2.得到图片
	        Graphics g = bi.getGraphics();
	        //3.设置图片的背影色
	        setBackGround(g);
	      //4.设置图片的边框
	        setBorder(g);
	        //5.在图片上画干扰线
	        drawRandomLine(g);
//	        
	        drawRandomNum((Graphics2D) g,authcode);
	        System.out.println(authcode);
	        
	        //9.设置响应头控制浏览器不要缓存
	        response.setDateHeader("expries", -1);
	        response.setHeader("Cache-Control", "no-cache");
	        response.setHeader("Pragma", "no-cache");
	        //10.将图片写给浏览器
	        ImageIO.write(bi, "jpg", response.getOutputStream());
		 
		 
	}
	private void drawRandomNum(Graphics2D g, String authcode) {
		// TODO Auto-generated method stub
		  // 设置颜色
        g.setColor(Color.RED);
        // 设置字体
        g.setFont(new Font("宋体", Font.BOLD, 20));
        g.drawString(authcode,  0, 20);
		
	}
	private void drawRandomLine(Graphics g) {
		// TODO Auto-generated method stub
		  // 设置颜色
        g.setColor(Color.GREEN);
        // 设置线条个数并画线
        for (int i = 0; i < 20; i++) {
            int x1 = new Random().nextInt(WIDTH);
            int y1 = new Random().nextInt(HEIGHT);
            int x2 = new Random().nextInt(WIDTH);
            int y2 = new Random().nextInt(HEIGHT);
            g.drawLine(x1, y1, x2, y2);
        }
		
	}
	private void setBorder(Graphics g) {
		// TODO Auto-generated method stub
		  // 设置边框颜色
        g.setColor(Color.BLACK);
        // 边框区域
        g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);
		
	}
	private void setBackGround(Graphics g) {
		// TODO Auto-generated method stub
		 // 设置颜色
        g.setColor(Color.YELLOW);
        // 填充区域
        g.fillRect(0, 0, WIDTH, HEIGHT);
	}

	 

}
